// pages/publish/index.js
import { request } from '../../request/http.js';
import regeneratorRuntime from '../../lib/runtime/runtime.js';
Page({
  data: {
    // 选择的图片数组
    chooseImgs: [],
    //每次点击都不同，所以重新访问就要置空
    name1: '',
    name2: ''
  },
  UploadImgs: [],
  // 全局接口要的参数 
  QueryParams: { cat_id: '', goods_name: '', goods_price: '', goods_number: '', school: '', goods_oldnew: '', goods_described: '', cat_one_id: '', cat_two_id: '', goods_small_logo: '', goods_small_logo2: '', goods_small_logo3: '',openid:'',status: '' },

  onLoad: function (options) {
    const openid=wx.getStorageSync('OPENID');
    const { name1, name2, cat_one_id, cat_two_id } = options;//从分类页面传递过来的
    this.setData({
      name1, name2
    }) 
    this.QueryParams.openid = openid || '';
    this.QueryParams.cat_one_id = cat_one_id || '';
    this.QueryParams.cat_two_id = cat_two_id || '';
    this.QueryParams.cat_id = cat_two_id || '';//使用cat)two_id里的当cat_id
    this.QueryParams.goods_name = this.QueryParams.goods_name || '';
    this.QueryParams.goods_price = this.QueryParams.goods_price || '';
    this.QueryParams.goods_number = this.QueryParams.goods_number || '';
    this.QueryParams.school = this.QueryParams.school || '';
    this.QueryParams.goods_oldnew = this.QueryParams.goods_oldnew || '';
    this.QueryParams.goods_described = this.QueryParams.goods_described || '';
    //模拟
    // this.QueryParams.goods_small_logo = 'http://img3m9.ddimg.cn/92/4/25581989-1_b_2.jpg';
    this.QueryParams.goods_small_logo = this.QueryParams.goods_small_logo || '';
    this.QueryParams.goods_small_logo2 = this.QueryParams.goods_small_logo || '';
    this.QueryParams.goods_small_logo3 = this.QueryParams.goods_small_logo || '';
    this.QueryParams.status = 0;
    console.log(options);
  },


  //获取name里值
  changeinputname(e){
    this.QueryParams.goods_name=e.detail.value;
  },
  //获取价格
  changeinputprice(e){
    this.QueryParams.goods_price = e.detail.value;
  },
  //获取数量
  changeinputnumber(e){
    this.QueryParams.goods_number = e.detail.value;
  },
  //获取学校
  changeinputschool(e){
    this.QueryParams.school = e.detail.value;
  },
  //新旧程度
  handleChangeoldnew(e){
    this.QueryParams.goods_oldnew = e.detail.value;
  },
  //描述商品
  changedescribed(e){
    this.QueryParams.goods_described = e.detail.value;
  },

  // 点击+号选择图片事件
  handleChooseImg() {
    wx.chooseImage({
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'],
      success: res => {
        // console.log(res.tempFilePaths);
        this.setData({
          chooseImgs: [...this.data.chooseImgs, ...res.tempFilePaths]
          // chooseImgs: this.data.chooseImgs.concat(res.tempFilePaths)
        })

      }
    })
  },
  // 移除图片
  handleRemoveImg(e) {
    const { index } = e.detail;
    let { chooseImgs } = this.data;
    chooseImgs.splice(index, 1);
    this.setData({ chooseImgs })
  },
  //点击发布
  handleSubmit() {
    const { name1, name2, chooseImgs} = this.data;
    const { goods_name, goods_price, goods_number, school, goods_oldnew, goods_described, goods_small_logo,openid } = this.QueryParams;
    // console.log(this.QueryParams);
    // console.log(this.data);
    // console.log(name1);
  //  const openid = wx.getStorageSync('OPENID');
   this.QueryParams.openid = wx.getStorageSync('OPENID');
  //  console.log(wx.getStorageSync('OPENID'));
   if (!(this.QueryParams.openid)) {
        wx.navigateTo({ url: '../authopenid/index', })
        return; 
    }
    if (!name1) {
      wx.showToast({ title: '请选择图书类型', icon: 'none', duration: 1000 })
      return;
    }
    if (!goods_name) {
      wx.showToast({ title: '请输入图书名称', icon: 'none', duration: 1000 })
      return;
    }
    if (!goods_price) {
      wx.showToast({ title: '请输入图书价格', icon: 'none', duration: 1000 })
      return;
    }
    if (!goods_number) {
      wx.showToast({ title: '请输入图书数量', icon: 'none', duration: 1000 })
      return;
    }
    if (!school) {
      wx.showToast({ title: '请输入学校', icon: 'none', duration: 1000 })
      return;
    }
    if (!goods_oldnew) {
      wx.showToast({ title: '请选择新旧程度', icon: 'none', duration: 1000 })
      return;
    }
    if (!goods_described.trim()) {
      wx.showToast({ title: '请稍微描述一下图书', icon: 'none', mask: true });
      return;
    }
    if (this.data.chooseImgs.length < 1) {
      wx.showToast({ title: '至少选择一张图片哦( ﾟ-ﾟ)っロ乾杯', icon: 'none', mask: true });
      return;
    }
    if (this.data.chooseImgs.length > 4) {
      wx.showToast({ title: '最多上传三张图片哦 ( ﾟ-ﾟ)っロ乾杯', icon: 'none', duration: 3000 });
      return;
    }
    wx.showLoading({
      title: '正在上传中',
      mask: true
    });
    //调用上传图片--使用递归方式
    this.uploadFile(0);
  },
  //上传图片
  uploadFile: function (index) {
    var that = this
    //如果所有图片都已经上传完，就不再往下执行
    if (index >= that.data.chooseImgs.length) {
      wx.hideLoading(); 
      console.log("提交完了！");   
      // console.log("下面是QueryParams");
      console.log(this.QueryParams);
      //6.可以做我们自己服务器的一些操作 发送请求向后端服务器      
      wx.request({
        url: 'http://192.168.43.159:8080/goods/goodsadd',
        data: this.QueryParams
      })
      //7.重置页面   将页面里面的置为空，方便下一次再输入进来
      this.setData({
        textVal: '',
        chooseImgs: []
      })
      wx.switchTab({//返回首页
        url: '../index/index',
      }) 
      return;  
    }
    wx.uploadFile({
      url: 'http://192.168.43.159:8080/upload/picture',
      filePath: that.data.chooseImgs[index],
      name: 'content',
      header: {
        "Content-Type": "multipart/form-data",
        'accept': 'application/json',
      },
      formData: ({
      }),
      success: (result) => {//请求图片变成网页连接，然后加在UploadImgs数组里
        console.log("成功！");
        console.log(result);
        let url = result.data;
        that.UploadImgs.push(url);
        console.log(that.UploadImgs[index]);
        if(index===0){
          that.QueryParams.goods_small_logo = that.UploadImgs[index];
        }
        if(index===1){
          that.QueryParams.goods_small_logo2 = that.UploadImgs[index];
        }
        if(index===2){
          that.QueryParams.goods_small_logo3 = that.UploadImgs[index];
        } 
        index++;
        that.uploadFile(index);       
      }
    })
   }
})
